/*
    FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.

    FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT 
    http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.

    ***************************************************************************
     *                                                                       *
     *    FreeRTOS tutorial books are available in pdf and paperback.        *
     *    Complete, revised, and edited pdf reference manuals are also       *
     *    available.                                                         *
     *                                                                       *
     *    Purchasing FreeRTOS documentation will not only help you, by       *
     *    ensuring you get running as quickly as possible and with an        *
     *    in-depth knowledge of how to use FreeRTOS, it will also help       *
     *    the FreeRTOS project to continue with its mission of providing     *
     *    professional grade, cross platform, de facto standard solutions    *
     *    for microcontrollers - completely free of charge!                  *
     *                                                                       *
     *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *
     *                                                                       *
     *    Thank you for using FreeRTOS, and thank you for your support!      *
     *                                                                       *
    ***************************************************************************


    This file is part of the FreeRTOS distribution.

    FreeRTOS is free software; you can redistribute it and/or modify it under
    the terms of the GNU General Public License (version 2) as published by the
    Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
    >>>NOTE<<< The modification to the GPL is included to allow you to
    distribute a combined work that includes FreeRTOS without being obliged to
    provide the source code for proprietary components outside of the FreeRTOS
    kernel.  FreeRTOS is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
    more details. You should have received a copy of the GNU General Public
    License and the FreeRTOS license exception along with FreeRTOS; if not it
    can be viewed here: http://www.freertos.org/a00114.html and also obtained
    by writing to Richard Barry, contact details for whom are available on the
    FreeRTOS WEB site.

    1 tab == 4 spaces!
    
    ***************************************************************************
     *                                                                       *
     *    Having a problem?  Start by reading the FAQ "My application does   *
     *    not run, what could be wrong?"                                     *
     *                                                                       *
     *    http://www.FreeRTOS.org/FAQHelp.html                               *
     *                                                                       *
    ***************************************************************************

    
    http://www.FreeRTOS.org - Documentation, training, latest versions, license 
    and contact details.  
    
    http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
    including FreeRTOS+Trace - an indispensable productivity tool.

    Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell 
    the code with commercial support, indemnification, and middleware, under 
    the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also
    provide a safety engineered and independently SIL3 certified version under 
    the SafeRTOS brand: http://www.SafeRTOS.com.
*/


/*
 * Creates all the demo application tasks, then starts the scheduler.  The WEB
 * documentation provides more details of the standard demo application tasks
 * (which just exist to test the kernel port and provide an example of how to use
 * each FreeRTOS API function).
 *
 * In addition to the standard demo tasks, the following tasks and tests are
 * defined and/or created within this file:
 *
 * "Check" task - This only executes every five seconds but has the highest
 * priority so is guaranteed to get processor time.  Its main function is to 
 * check that all the standard demo tasks are still operational. The check task
 * will toggle LED 3 (PB11) every five seconds so long as no errors have been
 * detected.  The toggle rate will increase to half a second if an error has 
 * been found in any task.
 *
 * "Echo" task - This is a very basic task that simply echoes any characters 
 * received on COM0 (USART1).  This can be tested by transmitting a text file
 * from a dumb terminal to the STM32 USART then observing or capturing the text
 * that is echoed back.  Missing characters will be all the more obvious if the 
 * file contains a simple repeating string of fixed width.
 *
 * Currently this demo does not include interrupt nesting examples.  High 
 * frequency timer and simpler nesting examples can be found in most Cortex-M3
 * demo applications.
 *
 * The functions used to initialise, set and clear LED outputs are normally 
 * defined in partest.c.  This demo includes two partest files, one that is 
 * configured for use with the Keil MCBSTM32 evaluation board (called 
 * ParTest_MCBSTM32.c) and one that is configured for use with the official
 * ST Eval board (called ParTest_ST_Eval.c).  One one of these files should be
 * included in the build at any one time, as appropriate for the hardware 
 * actually being used.
 */

/* Standard includes. */
#include <string.h>

/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
//#include "queue.h"

/* Library includes. */
#include "stm32f10x_it.h"
#include "stm32f10x.h"

/* Demo app includes. */
//#include "BlockQ.h"
//#include "integer.h"
//#include "flash.h"
//#include "partest.h"
//#include "semtest.h"
//#include "GenQTest.h"
//#include "QPeek.h"
//#include "recmutex.h"

/* Driver includes. */
//#include "STM32_USART.h"


/* The time between cycles of the 'check' task - which depends on whether the
check task has detected an error or not. */
#define mainCHECK_DELAY_NO_ERROR			( ( portTickType ) 5000 / portTICK_RATE_MS )
#define mainCHECK_DELAY_ERROR				( ( portTickType ) 500 / portTICK_RATE_MS )

/* The LED controlled by the 'check' task. */
#define mainCHECK_LED						( 3 )

/* Task priorities. */
#define mainSEM_TEST_PRIORITY				( tskIDLE_PRIORITY + 1 )
#define mainBLOCK_Q_PRIORITY				( tskIDLE_PRIORITY + 2 )
#define mainCHECK_TASK_PRIORITY				( tskIDLE_PRIORITY + 3 )
#define mainFLASH_TASK_PRIORITY				( tskIDLE_PRIORITY + 2 )
#define mainECHO_TASK_PRIORITY				( tskIDLE_PRIORITY + 1 )
#define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )
#define mainGEN_QUEUE_TASK_PRIORITY			( tskIDLE_PRIORITY )

/* COM port and baud rate used by the echo task. */
#define mainCOM0							( 0 )
#define mainBAUD_RATE						( 115200 )

/*-----------------------------------------------------------*/

/*
 * Configure the hardware for the demo.
 */
static void prvSetupHardware( void );


/*-----------------------------------------------------------*/
/* Described at the top of this file. */
static void Blink( void *pvParameters )
{





	for( ;; )
	{
		//FED

		GPIO_InitTypeDef  GPIO_InitStructure;

		  /* Enable the GPIO_LED Clock */
		 // RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_GPIOG, ENABLE);
		  RCC_APB2PeriphClockCmd(	RCC_APB2Periph_GPIOG , ENABLE );
		  /* Configure the GPIO_LED pin */
		  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
		  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
		 // GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
		 // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
		  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		  GPIO_Init(GPIOG, &GPIO_InitStructure);


		  GPIOG->BSRR= 0xFFFF;

		  //POK

		 // GPIO_InitTypeDef  GPIO_InitStructure;

		  /* Enable the GPIO_LED Clock */
		 // RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_GPIOG, ENABLE);
		  RCC_APB2PeriphClockCmd(	RCC_APB2Periph_GPIOE , ENABLE );
		  /* Configure the GPIO_LED pin */
		  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
		  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
		 // GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
		 // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
		  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		  GPIO_Init(GPIOE, &GPIO_InitStructure);


		  GPIOE->BSRR= 0xFFFF;



		  GPIO_SetBits(GPIOG,GPIO_Pin_7);


		vTaskDelay(1000/portTICK_RATE_MS);

		GPIO_ResetBits(GPIOG,GPIO_Pin_7);


		vTaskDelay(1000/portTICK_RATE_MS);
	}
}

int main( void )
{
#ifdef DEBUG
  debug();
#endif
  //SystemInit();
	/* Set up the clocks and memory interface. */
	prvSetupHardware();

	/* Start the standard demo tasks.  These are just here to exercise the
	kernel port and provide examples of how the FreeRTOS API can be used. */

	;


	/* Create the 'echo' task, which is also defined within this file. */
	xTaskCreate( Blink, ( signed char * ) "Blink", configMINIMAL_STACK_SIZE, NULL, mainECHO_TASK_PRIORITY, NULL );

	/* Create the 'check' task, which is also defined within this file. */


	//Blink(NULL);
    /* Start the scheduler. */
	vTaskStartScheduler();

    /* Will only get here if there was insufficient memory to create the idle
    task.  The idle task is created within vTaskStartScheduler(). */
	for( ;; );
}
/*-----------------------------------------------------------*/


/*-----------------------------------------------------------*/

/* Described at the top of this file. */

/*-----------------------------------------------------------*/

static void prvSetupHardware( void )
{
//	/* RCC system reset(for debug purpose). */
//	RCC_DeInit ();
//
//    /* Enable HSE. */
//	RCC_HSEConfig( RCC_HSE_ON );
//
//	/* Wait till HSE is ready. */
//	while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);
//
//    /* HCLK = SYSCLK. */
//	RCC_HCLKConfig( RCC_SYSCLK_Div1 );
//
//    /* PCLK2  = HCLK. */
//	RCC_PCLK2Config( RCC_HCLK_Div1 );
//
//    /* PCLK1  = HCLK/2. */
//	RCC_PCLK1Config( RCC_HCLK_Div2 );
//
//	/* ADCCLK = PCLK2/4. */
//	RCC_ADCCLKConfig( RCC_PCLK2_Div4 );
//
//    /* Flash 2 wait state. */
//	*( volatile unsigned long  * )0x40022000 = 0x01;
//
//	/* PLLCLK = 8MHz * 9 = 72 MHz */
//	RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_9 );
//
//    /* Enable PLL. */
//	RCC_PLLCmd( ENABLE );
//
//	/* Wait till PLL is ready. */
//	while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
//
//	/* Select PLL as system clock source. */
//	RCC_SYSCLKConfig (RCC_SYSCLKSource_PLLCLK);
//
//	/* Wait till PLL is used as system clock source. */
//	while (RCC_GetSYSCLKSource() != 0x08);

	/* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
	RCC_APB2PeriphClockCmd(	RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC
							| RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );

	/* Set the Vector Table base address at 0x08000000. */
	NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );

	NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );

	/* Configure HCLK clock as SysTick clock source. */
	SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );

	/* Initialise the IO used for the LED outputs. */
	//vParTestInitialise();

	/* SPI2 Periph clock enable */
	RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );
}
/*-----------------------------------------------------------*/

void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )
{
	/* This function will get called if a task overflows its stack.   If the
	parameters are corrupt then inspect pxCurrentTCB to find which was the
	offending task. */

	( void ) pxTask;
	( void ) pcTaskName;

	for( ;; );
}
/*-----------------------------------------------------------*/

void assert_failed( unsigned char *pucFile, unsigned long ulLine )
{
	( void ) pucFile;
	( void ) ulLine;

	for( ;; );
}

